package yj.scms.userservice.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import yj.scms.userservice.mapper.UserMapper;
import yj.scms.userservice.mapper.UserMapperXml;
import yj.scms.userservice.service.UserService;
import yj.scms.userserviceapi.pojo.UserEntity;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

	//User通用Mapper查询数据
	@Autowired
	private UserMapper usermapper;
	
	//Mybatis的XML查询数据
	@Autowired
	private UserMapperXml userMapperXml;
	
	@Override
	public List<UserEntity> pagehelperUserList(UserEntity user, Integer pageNo, Integer pageSize) {
		PageHelper.startPage(pageNo, pageSize);
		List<UserEntity> select = usermapper.select(user);
		return select;
	}
	
	@Override
	public List<Map<Object,Object>>  UserXmlHash(){
		
		return userMapperXml.queryTestMap();
	}

	@Override
	public List<UserEntity> UserXmlBean() {
		List<UserEntity> queryTestPojo = userMapperXml.queryTestPojo();
		return queryTestPojo;
	}

	@Override
	public PageInfo<UserEntity> findAll(UserEntity user, Integer pageNo, Integer pageSize) {
		PageHelper.startPage(pageNo, pageSize);
		Example example = new Example(UserEntity.class);
        if (!StringUtils.isEmpty(user.getName())) {
            example.createCriteria().andEqualTo("name", user.getName());
        }
        if (!StringUtils.isEmpty(user.getMobile())) {
            example.createCriteria().andEqualTo("mobile", user.getMobile());
        }
        List<UserEntity> users = usermapper.selectByExample(example);
        return new PageInfo<>(users);
	}

	@Override
	public Integer addUser(UserEntity user) {
		Integer a = usermapper.insert(user);
		return a;
	}

	@Override
	public UserEntity queryUserById(Long id){
		Example example = new Example(UserEntity.class);
        if (id!=null) {
            example.createCriteria().andEqualTo("id", id);
        }
        /*if (!StringUtils.isEmpty(user.getMobile())) {
            example.createCriteria().andEqualTo("mobile", user.getMobile());
        }*/
		UserEntity user = usermapper.selectByPrimaryKey(id);
		return user;
	}

	

}
